define(function (require,exports,module) {
    var myPub=require('./public');
    // 曝光可视区域元素
    var visualArea = require('./visualAreaInfo');

    var oFooter = $('.js_footer');                  // 底部
    var lazLoading = new myPub.lazLoadingImg();
    var oGoodsList = $('.js_goodsList');
    var oContain = $('.js_contain');
    var oGoTop = $('.js_goTop');                    // 回到顶部
    var oSideBox = $('.js_sideBox');		        // 侧边按钮框

    // var guessLikeUrl = '/shop/goods/guessLike';       //猜你喜欢商品列表地址
    var guessLikeUrl = '/m/g/1.0/group/listGoodsByName';       //猜你喜欢 最新 商品列表地址

    //设置本地存储
    var oOrderTableListLi = $('.js_OrderTableList').find('.js_orderDe');


    oOrderTableListLi.on('click',function() {
        // var index = $(this).index()
        var loca = $(this).attr('jumpName');
        localStorage.setItem("orderNav_index", loca);
    });

    $('.js_toMyAllOrder').on('click',function () { //全部订单
        localStorage.setItem("orderNav_index",0);
    });

    // 回到过顶部
    oGoTop.hammer().on('tap', function () {
        oContain.eq(0).stop().animate({scrollTop: '0'}, 250);

    });

    module.exports={
        init:function () {
            myInfo.initEvent();
            myInfoData.getGuessGoods(myInfoData.start,1, function () {
                myInfo.scrollMore();
            });
        }
    };



    var myInfo={
        time_Scroll: null,
        initEvent: function () {
            lazLoading.AddLazLoadingImg(oGoodsList[0]);
            // 导航选中设置
            oFooter.find('li').eq(3).addClass('active');
            oSideBox.stop().animate({'right': '-3.5rem'}, 300);

        },

        scrollMore: function () {
            var _this = this;
            var oContainScroll = $('.js_contain');
            var minScrHeight = 300;
            oContain.off('scroll');
            oContain.on('scroll',function (e) {
                var afterScroll = $(this).scrollTop();
                // console.log('hhhhaaa',afterScroll);
                _this.ShowGoTop(afterScroll);
                clearTimeout(_this.time_Scroll);
                _this.time_Scroll = setTimeout(function () {

                    // 统计曝光数据
                    ddjAction.viewGoods( oContain.find('.js_goodsList') );

                    if(oContainScroll[0].scrollHeight - afterScroll - oContainScroll.height() < minScrHeight){
                        if(!myInfoData.loadComplete){
                            myInfoData.start = myInfoData.start + myInfoData.limit;
                            myInfoData.getGuessGoods(myInfoData.start);
                        }
                    }
                },150)
            })
        },
        // 显示回到顶部按钮
        ShowGoTop: function (scrollTop) {
            scrollTop > 300 ? oSideBox.stop().animate({'right': 0}, 300) : oSideBox.stop().animate({'right': '-3.5rem'}, 300);
        },

    };

    var myInfoData = {
        start: 0,    //分页开始索引
        limit: 20,    //每次加载多少条数据
        loaded: false,  //有没有加载过
        loadNum: 0,     //加载过多少次,全部只能加载5次
        loadComplete: false,  //是否加载完
        //获取猜你喜欢商品
        getGuessGoods: function (start,type, fn) {
            // type是判断第一次进来还是滚动加载
            var _this = this;
            var data = { start: start, limit: _this.limit, groupName: 'H5_猜你喜欢'};

            //如果是第一次加载
            if(type == 1){
                data = { start: start, limit: _this.limit, isFirst: 1, groupName: 'H5_猜你喜欢'}
            }
            if(_this.loadNum < 5){
                G_JSON_GET(guessLikeUrl, data,function (res) {
                    // console.log('kkkkkkkk',res);
                    var _ret = res.ret;
                    if (_ret.code != '0') return;
                    _this.loadNum++;
                    var result = res.data;
                    if(type == 1){
                        //如果有返回的数据才显示猜你喜欢的部分
                        if(result && result.length > 0){
                            oContain.append(myInfoHtml.guessBoxHtml());
                            if(result.length >= _this.limit){
                                oContain.find('.js_loadingBtn').hide().eq(1).show();
                            }else {
                                _this.loadComplete = true;
                                oContain.find('.js_loadingBtn').hide().eq(0).show();
                            }
                            oContain.find('.js_goodsList').append(myInfoHtml.guessGoodsHtml(result));
                        }
                        //如果没数据返回则
                        else {
                            _this.loadComplete = true;
                            return;
                        }
                    }
                    else {
                        if(result && result.length > 0){
                            if(result.length >= _this.limit){
                                oContain.find('.js_loadingBtn').hide().eq(1).show();
                            }else {
                                _this.loadComplete = true;
                                oContain.find('.js_loadingBtn').hide().eq(0).show();
                            }
                            oContain.find('.js_goodsList').append(myInfoHtml.guessGoodsHtml(result));
                        }else {
                            _this.loadComplete = true;
                            oContain.find('.js_loadingBtn').hide().eq(0).show();
                        }
                    }
                    lazLoading.DestroyLazLoadingImg();
                    lazLoading.AddLazLoadingImg(oContain[0]);
                    if(fn)fn();
                },function (err) {})
            }
            else {
                _this.loadComplete = true;
                oContain.find('.js_loadingBtn').hide().eq(0).show();
            }

        }
    };

    var myInfoHtml = {
        //猜你喜欢部分
        guessBoxHtml: function () {
            var shtml = '';
            shtml = '<div class="myInfo_service myGuess">'+
                    '     <section class="service_title">' +
                    '         <p class="titleName">猜你喜欢</p>' +
                    '     </section>'+
                    '     <div class="goods_showChangeBox active js_goods_showTwo " style="margin-top: 0">'+
                    '          <div class="goods_showChange">'+
                    '              <ul class="goodsList clearfix js_goodsList">'+
                    '              </ul>'+
                    '          </div>'+
                    '          <div class="moreBtnBox" style="display: none1;">' +
                    '                <p class="loadingBtn js_loadingBtn" style="display: none;"><a>没有更多</a></p>' +
                    '                <p class="loadingBtn js_loadingBtn">' +
                    '                     <span class="icon_loading icoStyle icon-spinner3 myRotateRefresh"></span>' +
                    '                      <span>加载中</span>' +
                    '                  </p>' +
                    '            </div>'+
                    '      </div>'+
                    '</div>';
            return shtml;
        },
        //商品列表
        guessGoodsHtml: function (data) {
            var shtml = '';

            for(var i = 0; i < data.length; i++){
                var item = data[i];
                var quanpeiHtml = '';
                var goodsLabel = '';
                //如果有全赔图标则显示
                if(item.firstGueeImagePath) {
                    quanpeiHtml = '<div class="quanpei"><img src="https://img.sfddj.com/miniappImg/public/goodsInfo/'+item.firstGueeImagePath+'" alt="" /></div>'
                }

                shtml += '<li>' +
                    '          <a class="BoxSelf" href="'+ BASE_URL +'/h/goodspay/goodsView/'+data[i].goodsSn+'" requestid="'+item.requestId+'" goodsid="'+ data[i].id +'">' +
                    '                <figure>' +
                    '                     <img class="js_lazyImg" data-echo="'+HTTP_IMAGE_URL + item.goodsDefaultImage + smallImgArg200+'" src="'+ BASE_URL +'/static_v4/image/loadImg.jpg">' +
                    quanpeiHtml +
                    '                 </figure>' +
                    '                 <aside class="priceArea VBox Bflex1">' +
                    '                      <div class="Bflex1 priceArea_name">' +
                    '                           <h4>'+item.goodsName+'</h4>' +
                    '                           <p class="smallFont_tip">'+ item.goodsTitle +'</p>' +
                    '                       </div>' +
                    '                       <div class="priceArea_price">' +
                    '                            <p class="HBox priceAreaBox">' +
                    '                                 <span class="goodsPrice">￥'+ item.salePrice +'</span>' +
                    '                             </p>' +
                    '                         </div>' +
                    '                   </aside>' +
                    '            </a>' +
                    '            <p class="addCar js_addCar" pid="'+ item.productId +'"><em class="icon-shopcar_small"></em></p>' +
                    '       </li>';

            }
            return shtml;
        }
    }

    // 用户行为
    var ddjAction = {
        viewData: [],
        viewGoods: function (obj, iNow) {
            // 首页和分类区分
            var ddjActionPositionId = {};

            ddjActionPositionId.positionId = 2;
            // 个人中心 猜你喜欢
            // ddjActionPositionId.positionType = '个人中心-猜你喜欢';
            ddjActionPositionId.positionFirst = '个人中心';
            ddjActionPositionId.positionSecond = '猜你喜欢';

            // 统计曝光数据
            visualArea.viewEl.showViewEls( obj, function (data, dataJson) {
                ddjhoc.setData(data, 'rec_show', 'add', function () {
                    console.log('ddjhoc.params.rec_show.cate', ddjhoc.params);
                    if( ddjhoc.params.length != 0 ) ddjhoc.sendData( ddjhoc.params );

                    console.log('ddjhoc.ddjParams.rec_show.cate', ddjhoc.ddjParams);
                    if( ddjhoc.ddjParams.length != 0 ) ddjhoc.sendDDJData( ddjhoc.ddjParams );

                }, dataJson, ddjActionPositionId);
            });
        }
    }


});